package com.plusone.controller;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.plusone.action.ContactAction;
import com.plusone.model.Contact;
import com.plusone.toolset.InputVerifier;

public class ContactusController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// /**
	// *
	// */
	// public void doGet(HttpServletRequest request, HttpServletResponse
	// response) {
	// ServletContext context = getServletContext();
	// RequestDispatcher dispatcher = context
	// .getRequestDispatcher("/view/contactus.jsp");
	// try {
	// dispatcher.forward(request, response);
	// } catch (ServletException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name-field");
		String email = request.getParameter("email-field");
		String purpose = request.getParameter("purpose-field");

		Contact contact = new Contact();
		contact.setDisplayName(name);
		contact.setEmailAddress(email);
		contact.setPurpose(purpose);
		contact.setIpaddress(request.getRemoteAddr());

		ContactAction action = new ContactAction(new InputVerifier());

		if (action.IsValid(contact)) {
			action.Save(contact);
			action.sendMail(contact);
		} else {
			response.setContentType("text/plain");
			response.setStatus(400);// bad request
		}
	}
}
